<?php
/* 
 *   RichComments! v0.1
 *   The Wordpress Plugin that lets your blog subscribers attach photos and videos
 *   to their comments
 *   
 *   Copyright (C) 2007 Stefano Verna <stefano.verna@gmail.com>
 *   
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 2 of the License, or
 *   (at your option) any later version.
 *   
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *   
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.
 */

	function getServices() {
		return array(
			'flickr' => array(
				// we currently support only this family of URLs, more to come!
				// http://www.flickr.com/photos/junku-newcleus/417646359/...
				'urlfamily' => "/^(http:\/\/)?(www\.)?flickr\.com\/photos\/[^\/]+\/([0-9]{8,10})(\/(.+)?)?$/",
				'idmatch' => 3,
				'dataFetcher' => 'flickrFetcher',
				'mediaPrinter' => 'flickrPrinter'
			),
			'youtube' => array(
				// we currently support only this family of URLs, more to come!
				// http://www.youtube.com/watch?v=JX3VmDgiFnY
				'urlfamily' => "/^(http:\/\/)?(www\.)?youtube\.com\/watch\?(.*&)?v=([a-zA-Z0-9\-_]{11})(&.*)?$/",
				'idmatch' => 4,
				'dataFetcher' => 'youTubeFetcher',
				'mediaPrinter' => 'youTubePrinter'
			)
		);
	}
	
	function getID($type, $url) {
		$types = getServices();
		$data = $types[$type];
		preg_match($data['urlfamily'], $url, $matches);
		if (isset($matches[$data['idmatch']])) {
			return $matches[$data['idmatch']];
		}
		return "";
	}
	
	function youTubePrinter($id, $commentid) {
		global $_RichComments;
		
		if (!($rsp = file_get_contents('http://gdata.youtube.com/feeds/videos/'.$id))) {
			return "";
		}
		
		$xml = domxml_xmltree($rsp);
		$root = $xml->document_element();
		$curnode = $root->first_child();
		while ($curnode->tagname() != "title")
			$curnode = $curnode->next_sibling();
		
		return '<a href="'.$_RichComments->getCurrentPath().'/getvideo.php?id='.$id.'" class="thickbox_youtube rc_media" rel="comment_video'.$commentid.'" title="'.htmlspecialchars($curnode->get_content()).'" ><img src="http://img.youtube.com/vi/'.$id.'/default.jpg" ></a>';
	}
	
	function flickrPrinter($id, $commentid) {
		require_once("phpFlickr/phpFlickr.php");
		require("config.php");
		$mysql_connection_string = "mysql://".$RCSettings['Username'].":".$RCSettings['Password']."@".$RCSettings['Host']."/".$RCSettings['DBName'];
		
		$flickr = new phpFlickr($RCSettings['FlickrAPIKey']);
		if ($RCSettings['doCacheFlickrResults'])
			$flickr->enableCache("db", $mysql_connection_string, 6000, $RCSettings['CacheTable']);
		$sizes = $flickr->photos_getSizes($id);
		$info = $flickr->photos_getInfo($id);

		$max_width = 0;
		$src_thumb = $src_full = '';
		if (!$sizes) {
			return "";
		}
		
		foreach ($sizes as $size) {
			$w = intval($size['width']);
			$h = intval($size['height']);
			$label = $size['label'];
			
			if ($label == "Square")
				$src_thumb = $size['source'];
			else if ( $w <= 640 && $h <= 640 && $max_width < $w ) {
				$src_full = $size['source'];
				$max_width = $w;
			}
		}
		return '<a href="'.$src_full.'" class="thickbox_flickr rc_media" rel="comment_photo'.$commentid.'" title="'. htmlspecialchars($info['title']) .'" ><img src="'.$src_thumb.'" ></a>';
	}
	
	function flickrFetcher($url) {
		require_once("phpFlickr/phpFlickr.php");
		require("config.php");
		$mysql_connection_string = "mysql://".$RCSettings['Username'].":".$RCSettings['Password']."@".$RCSettings['Host']."/".$RCSettings['DBName'];
		
		$id = getID("flickr", $url);
		
		// create a new phpFlickr instance
		$flickr = new phpFlickr($RCSettings['FlickrAPIKey']);
		if ($RCSettings['doCacheFlickrResults'])
			$flickr->enableCache("db", $mysql_connection_string, 6000, $RCSettings['CacheTable']);
		$sizes = $flickr->photos_getSizes($id);
		$info = $flickr->photos_getInfo($id);
		
		if (!$sizes || !$info) {
			return false;
		}
		
		// grab photo thumbs and title
		$square_thumb = $small_thumb = $page_url = '';
		foreach ($sizes as $size) {
			if ($size['label'] == "Square")
				$square_thumb = $size['source'];
			if ($size['label'] == "Small")
				$small_thumb = $size['source'];
			if ($size['label'] == "Original")
				$page_url = $size['url'];
		}
		
		return array(
			'id' => $id,
			'previewImage' => $small_thumb,
			'thumb' => $square_thumb,
			'pageURL' => $page_url,
			'title' => $info['title']
		);
	}
	
	function youTubeFetcher($url) {
		$id = getID("youtube", $url);
		
		// easy :)
		$photo = 'http://img.youtube.com/vi/'.$id.'/default.jpg';
		$page = 'http://www.youtube.com/watch?v='.$id;
		$title = '';

		$rsp = file_get_contents('http://gdata.youtube.com/feeds/videos/'.$id);
		if (!$rsp) {
			return false;	
		}
		
		// grab video title from youtube new feed-based API
		$xml = domxml_xmltree($rsp);
		$root = $xml->document_element();
		$curnode = $root->first_child();
		while ($curnode->tagname() != "title") $curnode = $curnode->next_sibling();
		$title = $curnode->get_content();
		
		return array(
			'id' => $id,
			'previewImage' => $photo,
			'thumb' => $photo,
			'pageURL' => $page,
			'title' => $title
		);
	}
?>